package com.spring.dao.lmpl;

import com.spring.pojo.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;

@Repository
public class AccountDaolmpl implements com.spring.dao.AccountDao {
   @Autowired
    private QueryRunner queryRunner;

    // <bean>
     //        <consteuctor-agr /> <property/>
     //  </bean>

    //public AccountDaolmpl(QueryRunner queryRunner) {
     //   this.queryRunner = queryRunner;
   // }

    @Override
    public List<Account> findAll() {
        String sql = "select * from account";
        List<Account> query = null;
        try {
             query = queryRunner.query(sql, new BeanListHandler<Account>(Account.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }


        return query;
    }

    @Override
    public List<Account> findById(Integer id) {
        String sql = "select * from account where id = ?";
        List<Account> query = null;
        try {
            query = queryRunner.query(sql, new BeanListHandler<Account>(Account.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }


        return query;
    }

    @Override
    public void Save(Account account) {
        String sql = "insert into account valuse(null,?,?)";
        try {
            queryRunner.update(sql,account.getName(),account.getMoney());
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }

    @Override
    public void Update(Account account) {
        String sql = "update account set name=?,money=? where id = ?";
        try {
            queryRunner.update(sql,account.getName(),account.getMoney(),account.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }

    @Override
    public void Delete(Integer id) {
        String sql = "delete form account where id =?";
        try {
            queryRunner.update(sql, id);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
